package com.huiyujiaoyu.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.webkit.WebView;


import com.huiyujiaoyu.http.HttpApi;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.util.Constant;
import com.huiyujiaoyu.widget.LlkjToolBar;

import butterknife.BindView;

import static com.huiyujiaoyu.base.AppConfig.FGF;
import static com.huiyujiaoyu.util.StringUtils.extiWebView;


/**
 * author: dong.yj
 * created on: 2016/12/9 13:54
 * description:
 */
public class WebViewActivity extends BaseActivity {
    @BindView(R.id.webview_url)
    WebView webView;
    @BindView(R.id.toolbar)
    LlkjToolBar mToolbar;
    private String data,title;
    private int type;//1-url ，2-接口，3-h5内容 //4-自己拼接字符串   -1位传入过来的h5大数据

    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }


    @Override
    protected void initView() {
        super.initView();
        initWebView();
        title = getIntent().getStringExtra(Constant.DATA);
        data = getIntent().getStringExtra(Constant.DATA2);
        type = getIntent().getIntExtra(Constant.TYPE,-1);
        setToolbar(mToolbar);
        mToolbar.setToolBar(title,true,"", R.mipmap.icon_titlebar_back,false,"",LlkjToolBar.isNull);
        switch (type) {
            case 1://url
                webView.loadUrl(data);
                break;
            case 2://接口
                getApi();
                break;
            case 3://h5内容
                setDataWebView(data);
                break;
            case 4:
                String[] da = data.split(FGF);
                // webView.loadUrl(da[0]);
                break;
            case -1:
                setDataWebView(Constant.URL_DATA);
                break;
        }

    }

    public void setDataWebView(String data){
        webView.loadDataWithBaseURL(HttpApi.BASE_URL, data, "text/html", "UTF-8", null);
    }
    public void getApi(){

    }



    private void initWebView() {
        webView.setFocusable(true);//设置有焦点
        webView.setFocusableInTouchMode(true);//设置可触摸

        webView.getSettings().setSupportZoom(true);// 支持缩放
        webView.getSettings().setBuiltInZoomControls(true);// 支持缩放
    }

    @Override
    protected void initListener() {
        super.initListener();
    }


    /**
     *
     * @param c
     * 标题
     * @param title
     *   数据
     * @param data
     * 1-url ，2-接口，3-h5内容 //4-自己拼接字符串
     * @param type
     */
    public static void startActivity(Context c, String title, String data,int type) {
        Intent i = new Intent(c, WebViewActivity.class);
        i.putExtra(Constant.DATA, title);
        i.putExtra(Constant.DATA2, data);
        i.putExtra(Constant.TYPE, type);
        c.startActivity(i);
    }

    public static void startActivity(Context c, String title, String data) {
        Intent i = new Intent(c, WebViewActivity.class);
         Constant.URL_DATA = data;
        i.putExtra(Constant.DATA, title);
        c.startActivity(i);
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        extiWebView(webView);
    }

    @SuppressLint("NewApi")
    @Override
    public void onPause() {
        super.onPause();
        webView.onPause();
    }

    @SuppressLint("NewApi")
    @Override
    public void onResume() {
        super.onResume();
        webView.onResume();
    }

    @Override
    public void onNext(Object o, int httpcode) {
        super.onNext(o, httpcode);

    }
}
